package ophiux.wechatpay.sdk.base;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import ophiux.wechatpay.sdk.OphiuxWeChatPaySDKException;
import ophiux.wechatpay.sdk.common.utils.json.JSONWriter;
import ophiux.wechatpay.sdk.util.AesUtil;

public abstract class OphiuxMedicalPayBaseRequest<T extends OphiuxMedicalPayResponse> {
	
	public String getApiMethod() {
		return "POST";
	}

	public String getApiVersion() {
		return "1.0";
	}
	
	public abstract Map getTextParam();
	
	public abstract Class<T> getResponseClass();

	
	
	
	public String toRequestParam(Map publicParams,Map businessParams) throws OphiuxWeChatPaySDKException{
		publicParams.putAll(businessParams);
		String sign = (String) publicParams.get("sign");
		String key = (String) publicParams.get("key");
		Iterator<Map.Entry<String, String>> it = publicParams.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, String> entry = it.next();
            if(entry.getValue() == null)
                it.remove();//使用迭代器的remove()方法删除元素  
        }
        publicParams.remove("sign");
        publicParams.remove("key");
		String jsonParam = new JSONWriter().write(publicParams);
		
		Map<String,Object> param = new HashMap<String, Object>();
        param.put("param", AesUtil.encrypt(jsonParam,key.substring(8, 40)));
        param.put("appId",publicParams.get("appId"));
        param.put("sign", sign);
		
		return new JSONWriter().write(param);
	}


	 
	
}
